package errors

import (
	"fmt"

	"gitee.com/yxd1990/golang-common/constants"
)

type E struct {
	msg  string
	code constants.ErrorCode
}

func (f *E) Error() string { return f.msg }

func (f *E) Code() constants.ErrorCode {
	return f.code
}

func Emit(formatter string, params ...interface{}) error {
	return &E{
		msg:  fmt.Sprintf(formatter, params...),
		code: constants.ErrMsg,
	}
}

func EmitCode(code constants.ErrorCode) error {
	return EmitMsgAndCode("", code)
}

func EmitMsgAndCode(msg string, code constants.ErrorCode) error {
	return &E{
		msg:  msg,
		code: code,
	}
}
